define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Productmanager_waitsale, GoPage, $ionicSideMenuDelegate,GoDialog) {
		$scope.scope = new Object();
		$scope.filter = new Object();
		$scope.brand = new Array();
		$scope.cat = new Array();
		//跳转到添加分类页面
		$scope.onshow_productmanager_waitsale_edit = function() {
				GoPage.go_states("productmanager_waitsale_edit");
			}
			//打开侧边的筛选菜单
		$scope.openRightMenu = function() {
			$ionicSideMenuDelegate.toggleRight();
		}

		//4个导航
		$scope.onshow_productmanager = function(index) {
			if(index == 1) GoPage.go_states("productmanager_sale");
			if(index == 2) GoPage.go_states("productmanager_brand");
			//	if(index==3) GoPage.go_states("productmanager_waitsale");
			if(index == 4) GoPage.go_states("productmanager_sort");
		}
		$scope.sortUp = "ion-ios-arrow-thin-up";
		$scope.sortDown = "ion-ios-arrow-thin-down";
		$scope.sortUp_p = "ion-ios-arrow-thin-up";
		$scope.sortDown_p = "ion-ios-arrow-thin-down";
		//boolean数字，前面2个用来控制价格排序，后面2个用来控制销量排序
		$scope.isHidden_arrs = [true, true, true, true];
		$scope.sortPrice = function() {
			//价格从高到低
			if($scope.isHidden_arrs[0]) {
				$scope.isHidden_arrs[0] = false;
				$scope.isHidden_arrs[1] = true;
				$scope.sortDown = "ion-ios-arrow-thin-up";
				Productmanager_waitsale.goods_search('key', 2, 'filter', function(data) {
					$scope.datas = data.goods;
				})
			} else {
				$scope.isHidden_arrs[0] = true;
				$scope.isHidden_arrs[1] = false;
				$scope.sortDown = "ion-ios-arrow-thin-down";
				Productmanager_waitsale.goods_search('key', 3, 'filter', function(data) {
					$scope.datas = data.goods;
				})
			}

		}
		$scope.sortProduct = function() {
			//销售量从高到低
			if($scope.isHidden_arrs[2]) {
				$scope.isHidden_arrs[2] = false;
				$scope.isHidden_arrs[3] = true;
				$scope.sortDown_p = "ion-ios-arrow-thin-up";
				Productmanager_waitsale.goods_search('key', 4, 'filter', function(data) {
					$scope.datas = data.goods;
				})
			} else {
				$scope.isHidden_arrs[2] = true;
				$scope.isHidden_arrs[3] = false;
				$scope.sortDown_p = "ion-ios-arrow-thin-down";
				Productmanager_waitsale.goods_search('key', 5, 'filter', function(data) {
					$scope.datas = data.goods;
				})
			}
		}
		$scope.goods_update = function(goods_id) {
			console.log(goods_id);
			Productmanager_waitsale.goods_update(goods_id, false, function(data) {

			})
		}
		$scope.goods_delete = function(goods_id) {
			console.log(goods_id);
			Productmanager_waitsale.goods_delete(goods_id, function(data) {

			})
		}

		$scope.input_data = function() {
			var i = $scope.brand.length;
			$scope.brand[i] = new Object();
			$scope.brand[i]['brand_id'] = $scope.scope.brand_id;
			var j = $scope.cat.length;
			$scope.cat[j] = new Object();
			$scope.cat[j]['cat_id'] = $scope.scope.cat_id1;
			$scope.cat[j + 1] = new Object();
			$scope.cat[i + 1]['cat_id'] = $scope.scope.cat_id2;
			$scope.cat[j + 2] = new Object();
			$scope.cat[i + 2]['cat_id'] = $scope.scope.cat_id3;
			$scope.filter.bn = '商品编码';
			$scope.filter.brand_id = $scope.brand;
			$scope.filter.cat_id = $scope.cat;
			$scope.filter.price_frome = $scope.scope.low;
			$scope.filter.price_to = $scope.scope.high;
			$scope.filter.custom_cat_id = '店铺自定义分类ID';
			console.log($scope.filter);
		}
		$scope.reset = function() {
			$scope.scope.key = '';
			$scope.scope.brand_id = '';
			$scope.scope.cat_id1 = '';
			$scope.scope.cat_id2 = '';
			$scope.scope.cat_id3 = '';
			$scope.scope.low = '';
			$scope.scope.high = '';
			$scope.filter = new Object();
			$scope.brand = new Array();
			$scope.cat = new Array();
		}
		$scope.submit = function() {
			$scope.input_data();
			Productmanager_waitsale.goods_search($scope.scope.key, 1, 'filter', function(data) {
				$scope.datas = data.goods;
				$scope.filter = new Object();
				$scope.brand = new Array();
				$scope.cat = new Array();
				//				$scope.reset();
				//				console.log($scope.filter);
			})
		}

		$scope.init_data = function() {
			Productmanager_waitsale.goods_search('key', 'orderby', 'filter', function(data) {
				$scope.datas = data.goods;
			})
		}
		$scope.init_data();
		$scope.doSelectbrand = function(){
			//id是值 name是显示
			GoDialog.showSelect("品牌",$scope.brands,$scope.scope.brand_select,function(data){
				$scope.scope.brand_select = data;
				$scope.filter.brand_id[0] = data.id;
			});
		}
		$scope.doSelectCat1 = function(){
			//id是值 name是显示
			GoDialog.showSelect("大分类",$scope.cat1_items,$scope.input.cat1,function(data){
				$scope.input.cat1 = data;
			});
		}
		$scope.doSelectCat2 = function(){
			//id是值 name是显示
			GoDialog.showSelect("中分类",$scope.cat2_items,$scope.input.cat2,function(data){
				$scope.input.cat2 = data;
			});
		}
		$scope.doSelectCat3 = function(){
			//id是值 name是显示
			GoDialog.showSelect("小分类",$scope.cat3_items,$scope.input.cat3,function(data){
				$scope.input.cat3 = data;
			});
		}
	}

	controller.$inject = ['$scope', 'Productmanager_waitsale', 'GoPage', '$ionicSideMenuDelegate',"GoDialog"];

	return app.registerController("productmanager_waitsalectrl",controller);
});